package views
{
	import flash.utils.setTimeout;
	
	import inerface.ISelfPathListPanel;
	
	import main.ViewSubBase;
	
	import panel.selfPathList.SelfPathListPanel;
	
	import struct.Hotel;
	import struct.Restaurant;
	import struct.Scenic;
	import struct.ScenicShop;
	import struct.SelfPath;
	
	public class SelfPathListView extends ViewSubBase
	{
		private var _panel:ISelfPathListPanel;
		
		public function SelfPathListView()
		{
		}
		
		public override function show():void
		{
			render();
		}
		
		protected override function render():void
		{
			if(_panel == null){
				_panel = new SelfPathListPanel;
				_panel.init(view.layout.stage,view.resourceLoader,view.lang,view.uri);
				
				_panel.onShowDetail = showPath;
				_panel.onShowSys = showSys;
				_panel.onChangeChoosen = changeChoosenPath;
				_panel.deletePath = deletePath;
				
				_panel.changeData(data.choosePath.selfPathList);				
				_panel.refreshState = data.choosePath.currentPathID;
			}
			
			this.addView(_panel);
		}
		
		public function showPath(obj:SelfPath):void
		{
			//			if(obj.info[0] is String){
			//				obj.initList();
			//			}
			
			for(var i:int = 0;i < obj.info.length;i++){
				
				if(obj.info[i] is Array){
					
					for(var j:int = 0;j < obj.info[i].length;j++){
						if(obj.info[i][j] is Restaurant || obj.info[i][j] is Scenic || obj.info[i][j] is ScenicShop || obj.info[i][j] is Hotel){
							
							obj.info[i][j].init(view.uri,data.xmlLoaderManager);
							obj.info[i][j].initWithData(obj.info[i][j]);
						}
					}
				}
			}
			
			view.selfPathEdit.info = obj;
			view.selfPathEdit.show();
		}
		
		private function showSys():void
		{
			view.sysRecommendPathList.show();
		}
		
		private function deletePath(info:SelfPath):void
		{
			view.alert.showAlert("确认删除此路线？","确定","取消",
				function ():void {
					if(data.choosePath.currentPathID == info.id){
						setTimeout(function ():void{
							view.alert.showAlert("无法删除当前正在编辑的路线，请关闭编辑图标再尝试删除","返回");
						},500);
						return;
					}
					data.choosePath.deltePathAndSave(info.id);
					_panel.changeData(data.choosePath.selfPathList);
					_panel.refreshState = data.choosePath.currentPathID;
				}
			);
		}
		
		private function changeChoosenPath(info:SelfPath,bool:Boolean):void
		{
			if(bool == true){
				data.choosePath.saveChoosenPathID(info.id);
			}else{
				data.choosePath.addANewPath();
				
				_panel.changeData(data.choosePath.selfPathList);			
				_panel.refreshState = data.choosePath.currentPathID;
			}
		}
		
		public override function clear():void
		{
			_panel.clear();
			_panel = null;
		}
		
		public override function close():void
		{
			closeView(this);
		}
	}
}